﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using WebServer.DBUtility;

namespace WebServer.DBAccess
{
    public class SendCodeDA
    {
        /// <summary>
        /// 记录可用验证
        /// </summary>
        /// <param name="Email"></param>
        /// <param name="Mobile"></param>
        /// <param name="Code"></param>
        /// <param name="EndTime"></param>
        /// <param name="Usable"></param>
        /// <returns></returns>
        public string Insert(string Email, string Mobile, string Code, DateTime EndTime, bool Usable)
        {
            string request = string.Empty; 

            try
            {
                SQLHelper sqlHelp = new SQLHelper();
                using (IDataReader reader = sqlHelp.ExecuteReader("sp_S_Security_i", new object[] { Email, Mobile, Code, EndTime, Usable }))
                {
                    while (reader.Read())
                    {
                        request = reader[0].ToString();
                    }
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
                request = ex.ToString();
            }
            return request;
        }

        /// <summary>
        /// 查询可用性
        /// </summary>
        /// <param name="Mobile"></param>
        /// <returns></returns>
        public string HasCode(string Mobile)
        {
            string request = "";

            try
            {
                SQLHelper sqlHelp = new SQLHelper();
                using (IDataReader reader = sqlHelp.ExecuteReader("sp_S_Security_has", new object[] { Mobile, DateTime.Now.AddMinutes(1) }))
                {
                    while (reader.Read())
                    {
                        request = reader[0] != DBNull.Value ? (reader[0].ToString()) : "";
                    }
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
            }
            return request;
        }

        /// <summary>
        /// 认证
        /// </summary>
        /// <param name="Email"></param>
        /// <param name="Mobile"></param>
        /// <param name="Code"></param>
        /// <returns></returns>
        public bool SecurityLogin(string Email, string Mobile, string Code)
        {
            bool request = false;

            try
            {
                SQLHelper sqlHelp = new SQLHelper();
                using (IDataReader reader = sqlHelp.ExecuteReader("sp_S_Security_Login", new object[] { Email, Mobile, Code, DateTime.Now }))
                {
                    while (reader.Read())
                    {
                        request = reader[0] != DBNull.Value ? (reader[0].ToInt32() > 0 ? true : false) : false;
                    }
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
            }
            return request;
        }

        /// <summary>
        /// 认证
        /// </summary>
        /// <param name="Email"></param>
        /// <param name="Mobile"></param>
        /// <param name="Code"></param>
        /// <returns></returns>
        public bool LoginCode(string Email, string Mobile, string Code)
        {
            bool request = false;

            try
            {
                SQLHelper sqlHelp = new SQLHelper();
                using (IDataReader reader = sqlHelp.ExecuteReader("[sp_S_Code_Login]", new object[] { Email, Mobile, Code, DateTime.Now }))
                {
                    while (reader.Read())
                    {
                        request = reader[0] != DBNull.Value ? (reader[0].ToInt32() > 0 ? true : false) : false;
                    }
                    reader.Close();
                }
            }
            catch (Exception ex)
            {
            }
            return request;
        }
    }
}
